*************************************************************************
* Table 2: effects on value disbursed, value received, and leakage    
* This table reports estimated treatment effects on the value of commodities disbursed by the government, received by recipients, and the difference in endline one (January - March).
*************************************************************************

eststo clear

**************************************************************
* 0) mean entitlement
**************************************************************

*20180828 correct for rural/urban status for two blocks
use "${SurveyDataDir}\JH_ePOS_HH_DataforAnalysis.dta",clear



svyset [pw = pweight]


count if ghost_final == 1
scalar ghosts = r(N)
scalar obs = 3960   

*Get relative weights of AAY and PH rationcard holders by whether RC is in an urban area
sum pweight if rationcardtype == "AAY" & isurban == 0
scalar AAY_weight0 = r(sum)

sum pweight if rationcardtype == "PH" & isurban == 0
scalar PH_weight0 = r(sum)

sum pweight if rationcardtype == "AAY" & isurban == 1
scalar AAY_weight1 = r(sum)

sum pweight if rationcardtype == "PH" & isurban == 1
scalar PH_weight1 = r(sum)

*Create seeded status
gen seeded_ind     = 1 if uidcount > 0 & !missing(uidcount)
replace seeded_ind = 0 if uidcount == 0  

*Create interaction variable
gen interaction = treatment * seeded_ind

***************************************************************
*store mean entitlement in place holder regression results (does not output regression results - only used for outputting entitlement row)

loc ration "total rice wheat sugar salt kero"

foreach rat of local ration {
preserve

keep value_`rat'_mar17 value_`rat'_feb17 value_`rat'_jan17 treatment strata pweight block_code uid rationcardtype stat_ent_value_`rat'* rationcardtype isurban interaction
gen value_`rat'3 = value_`rat'_mar17
gen value_`rat'2 = value_`rat'_feb17
gen value_`rat'1 = value_`rat'_jan17
drop value_`rat'_mar17 value_`rat'_feb17 value_`rat'_jan17
rename stat_ent_value_`rat'_mar17 stat_ent_value_`rat'3
rename stat_ent_value_`rat'_feb17 stat_ent_value_`rat'2
rename stat_ent_value_`rat'_jan17 stat_ent_value_`rat'1
reshape long value_`rat' stat_ent_value_`rat', i(uid) j(month)

count
scalar obs_all = 3*3960

*Statutory NIC entitlement
qui svy: mean stat_ent_value_`rat' if rationcardtype == "AAY" & isurban == 0
qui estat sd
matrix MeanAAY0s = r(mean)

qui svy: mean stat_ent_value_`rat' if rationcardtype == "PH" & isurban == 0
qui estat sd
matrix MeanPH0s = r(mean)

qui svy: mean stat_ent_value_`rat' if rationcardtype == "AAY" & isurban == 1
qui estat sd
matrix MeanAAY1s = r(mean)

qui svy: mean stat_ent_value_`rat' if rationcardtype == "PH" & isurban == 1
qui estat sd
matrix MeanPH1s = r(mean)

di in red "`rat'"
matrix list MeanAAY0s
matrix list MeanAAY1s
matrix list MeanPH0s
matrix list MeanPH1s

scalar avg_stat_ent_value_`rat' = (AAY_weight0*`=MeanAAY0s[1,1]' + PH_weight0*`=MeanPH0s[1,1]' + AAY_weight1*`=MeanAAY1s[1,1]' + PH_weight1*`=MeanPH1s[1,1]')/(AAY_weight0 + PH_weight0 + AAY_weight1 + PH_weight1)

qui svy: mean value_`rat' if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)

*place holder regression
eststo `rat'_v1BL: xi: reg value_`rat' treatment interaction i.strata [pw = pweight], cluster(block_code)
estadd scalar stat_ent_mean = avg_stat_ent_value_`rat'

restore

}

di in red "EL1"

*****************************************************
* Start multi panel table with Mean Entitlement row

loc tabname "${OutputDir}/Table2.tex" 
cd "${adoDir}"

MultiPartTabStart, ///
			ncol(6) tabname(`tabname') ///
			colnames("Total" "Rice" "Wheat" "Sugar" "Salt" "Kerosene") width("\hsize")			


MultiPartTabPanelStartEntitle, ///
			ncol(6) tabname(`tabname') ///
			panelstring("")
			
			estimates restore *total_v1BL
			estimates restore *rice_v1BL
			estimates restore *wheat_v1BL
			estimates restore *sugar_v1BL
			estimates restore *salt_v1BL
			estimates restore *kero_v1BL
			
MultiPartTabPanelEndEntitle, ///
				ncol(6) tabname(`tabname') ///
				models("*total_v1BL *rice_v1BL *wheat_v1BL *sugar_v1BL *salt_v1BL *kero_v1BL") ///
				drop(treatment _Ist* _cons interaction) ///
				cells(b) ///
				stats(stat_ent_mean, label("\textit{Mean entitlement}") fmt(0))
	
eststo cr	
**************************************************************
* 1) disbursement
**************************************************************

********************************************
use "${AdminDataDir}\allocation_FPSlevel_jan17_mar17.dta",clear

******************************************************
*Analysis
******************************************************
 
*****************************
* Regression using data pooled across three endline months
*****************************

local ration "rice wheat salt sugar kero"

local tflist ""
local tflist_noBL ""
local tflist_trend ""
local tflist_trend_noBL "" 

foreach rat of local ration{
scalar obs=26801

gen bl_var = dis_value_perRC_`rat'_y0
egen bl_var_mean = mean(bl_var)
gen bl_var_mi = missing(bl_var)
replace bl_var = bl_var_mean if bl_var_mi == 1

*Estimation
preserve
keep dis_value_perRC_`rat'_*17 ent_value_perRC_`rat'_*17 rc_count_*17 treatment strata block_code bl_var bl_var_mi admin_dealer_id
rename dis_value_perRC_`rat'_jan17 dis_value_perRC1
rename dis_value_perRC_`rat'_feb17 dis_value_perRC2
rename dis_value_perRC_`rat'_mar17 dis_value_perRC3
rename ent_value_perRC_`rat'_jan17 ent_value_perRC1
rename ent_value_perRC_`rat'_feb17 ent_value_perRC2
rename ent_value_perRC_`rat'_mar17 ent_value_perRC3
rename rc_count_jan17 rc_count1
rename rc_count_feb17 rc_count2
rename rc_count_mar17 rc_count3

reshape long dis_value_perRC ent_value_perRC rc_count, i(admin_dealer_id) j(month)

gen treatmentXmonth=treatment*month

qui: summarize dis_value_perRC if treatment == 0  [aw=rc_count]
matrix Mean0 = r(mean)

qui: mean ent_value_perRC if treatment == 0 
qui estat sd
matrix Mean0e = r(mean)



eststo pool_`rat'_v1BL: xi: reg dis_value_perRC treatment bl_var bl_var_mi i.strata [aweight=rc_count] , cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar stat_ent_mean = `=Mean0e[1,1]'
estadd scalar percent_obs=100*e(N)/obs


tempfile pool_`rat'_v1BL
parmest, label saving("`pool_`rat'_v1BL'")
local tflist "`tflist' `pool_`rat'_v1BL'"

restore
drop bl_*
}

*Total value
preserve
gen bl_var = dis_value_perRC_total_y0
egen bl_var_mean = mean(bl_var)
gen bl_var_mi = missing(bl_var)
replace bl_var = bl_var_mean if bl_var_mi == 1

keep dis_value_perRC_total_*17 ent_value_perRC_total_*17 rc_count_*17 treatment strata block_code bl_var bl_var_mi admin_dealer_id
rename dis_value_perRC_total_jan17 dis_value_perRC1
rename dis_value_perRC_total_feb17 dis_value_perRC2
rename dis_value_perRC_total_mar17 dis_value_perRC3
rename ent_value_perRC_total_jan17 ent_value_perRC1
rename ent_value_perRC_total_feb17 ent_value_perRC2
rename ent_value_perRC_total_mar17 ent_value_perRC3
rename rc_count_jan17 rc_count1
rename rc_count_feb17 rc_count2
rename rc_count_mar17 rc_count3

reshape long dis_value_perRC ent_value_perRC rc_count, i(admin_dealer_id) j(month)

gen treatmentXmonth=treatment*month

qui: summarize dis_value_perRC if treatment == 0  [aw=rc_count]
matrix Mean0 = r(mean)

qui: mean ent_value_perRC if treatment == 0 
qui estat sd
matrix Mean0e = r(mean)
eststo pool_total_v1BL: xi: reg dis_value_perRC treatment bl_var bl_var_mi i.strata [aweight=rc_count], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar stat_ent_mean = `=Mean0e[1,1]'
estadd scalar percent_obs=100*e(N)/obs
drop bl_*
restore

*Pooled with baseline
preserve
dsconcat `tflist',subset(parm p in 1) 
qqvalue p, method(yekutieli) qvalue(qval)
mkmat qval

*MHT-adding FDR adjusted p values to stored estimates
forval i = 1/5 {
local rat: word `i' of `ration'
estimates restore pool_`rat'_v1BL


mat bet=e(b)                   

mat bet[1,1]=qval[`i',1]             
mat list bet      
estadd matrix qvalue = bet               
                                            
mat list e(qvalue)
}
	
restore	
********** Panel for  Value disbursed 	
			
MultiPartTabPanelStart, ///
			ncol(6) tabname(`tabname') ///
			panelstring("Panel A: Value disbursed")
			
			estimates restore pool_total_v1BL
			estimates restore pool_rice_v1BL
			estimates restore pool_wheat_v1BL
			estimates restore pool_sugar_v1BL
			estimates restore pool_salt_v1BL
			estimates restore pool_kero_v1BL
			
MultiPartTabPanelEnd, ///
				ncol(6) tabname(`tabname') ///
				models("pool_total_v1BL pool_rice_v1BL pool_wheat_v1BL pool_sugar_v1BL pool_salt_v1BL pool_kero_v1BL") ///
				keep(treatment) varlabels(treatment "Treatment" , elist(treatment \addlinespace) ) ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ])  keep(treatment) fmt(2)) ) ///
				starlevels( * 0.10 ** 0.05 *** 0.01) ///
				stats(control_mean N percent_obs, label("Control mean" "Observations" "\% of frame") fmt(%12.2g %15.0fc 0))
	

**************************************************************
* 2) receipt
**************************************************************

*20180828 correct for rural/urban status for two blocks
use "${SurveyDataDir}/JH_ePOS_HH_DataforAnalysis.dta",clear

*******************************************************************************
* Keep if surveyed or classified as ghost in a temp dataset 
keep if ss_code == "SS01" | ghost_final == 1

preserve
tempfile HHdata_withghosts
save `HHdata_withghosts'
restore

*Create seeded status
gen seeded_ind     = 1 if uidcount > 0 & !missing(uidcount)
replace seeded_ind = 0 if uidcount == 0  

*Create interaction variable
gen interaction = treatment * seeded_ind

*******************************************************************************
svyset [pw = pweight]


count if ghost_final == 1
scalar ghosts = r(N)
scalar obs = 3960   

*Get relative weights of AAY and PH rationcard holders by whether RC is in an urban area
sum pweight if rationcardtype == "AAY" & isurban == 0
scalar AAY_weight0 = r(sum)

sum pweight if rationcardtype == "PH" & isurban == 0
scalar PH_weight0 = r(sum)

sum pweight if rationcardtype == "AAY" & isurban == 1
scalar AAY_weight1 = r(sum)

sum pweight if rationcardtype == "PH" & isurban == 1
scalar PH_weight1 = r(sum)



*****************************
* Regression using data pooled across three endline months
*****************************

local tflist ""
local tflist_noBL ""
local tflist_trend ""
local tflist_trend_noBL ""


loc ration "rice wheat sugar salt kero"
foreach rat of local ration {

gen bl_var = value_`rat'_y0
egen bl_var_mean = mean(bl_var)
gen bl_var_mi = missing(bl_var)
replace bl_var = bl_var_mean if bl_var_mi == 1

*Estimation
preserve


keep value_`rat'_mar17 value_`rat'_feb17 value_`rat'_jan17 bl_var bl_var_mi treatment strata pweight block_code uid rationcardtype stat_ent_value_`rat'* rationcardtype isurban interaction
gen value_`rat'3 = value_`rat'_mar17
gen value_`rat'2 = value_`rat'_feb17
gen value_`rat'1 = value_`rat'_jan17
drop value_`rat'_mar17 value_`rat'_feb17 value_`rat'_jan17
rename stat_ent_value_`rat'_mar17 stat_ent_value_`rat'3
rename stat_ent_value_`rat'_feb17 stat_ent_value_`rat'2
rename stat_ent_value_`rat'_jan17 stat_ent_value_`rat'1
reshape long value_`rat' stat_ent_value_`rat', i(uid) j(month)

count
scalar obs_all = 3*3960

*Statutory NIC entitlement
qui svy: mean stat_ent_value_`rat' if rationcardtype == "AAY" & isurban == 0
qui estat sd
matrix MeanAAY0s = r(mean)

qui svy: mean stat_ent_value_`rat' if rationcardtype == "PH" & isurban == 0
qui estat sd
matrix MeanPH0s = r(mean)

qui svy: mean stat_ent_value_`rat' if rationcardtype == "AAY" & isurban == 1
qui estat sd
matrix MeanAAY1s = r(mean)

qui svy: mean stat_ent_value_`rat' if rationcardtype == "PH" & isurban == 1
qui estat sd
matrix MeanPH1s = r(mean)

scalar avg_stat_ent_value_`rat' = (AAY_weight0*`=MeanAAY0s[1,1]' + PH_weight0*`=MeanPH0s[1,1]' + AAY_weight1*`=MeanAAY1s[1,1]' + PH_weight1*`=MeanPH1s[1,1]')/(AAY_weight0 + PH_weight0 + AAY_weight1 + PH_weight1)

gen treatmentXmonth=treatment*month

qui svy: mean value_`rat' if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)


eststo `rat'poolBL1: xi: reg value_`rat' treatment interaction i.strata bl_var bl_var_mi [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar stat_ent_mean = avg_stat_ent_value_`rat'
estadd scalar percent_obs = 100*e(N)/obs_all

tempfile `rat'poolBL1
parmest, label saving("``rat'poolBL1'")
local tflist "`tflist' ``rat'poolBL1'"


restore
drop bl_var*
}

*Total value
count
scalar obs = 3960

gen bl_var = value_total_y0
egen bl_var_mean = mean(bl_var)
gen bl_var_mi = missing(bl_var)
replace bl_var = bl_var_mean if bl_var_mi == 1


preserve
keep uid value_*17 bl_var bl_var_mi treatment strata pweight block_code ss_code  stat_ent_value_*17 rationcardtype isurban ghost_final b20_adr_seed_hh_0_1 interaction
rename stat_ent_value_total_mar17 stat_ent_value_total3
rename stat_ent_value_total_feb17 stat_ent_value_total2
rename stat_ent_value_total_jan17 stat_ent_value_total1
rename value_total_mar17 value_total3 
rename value_total_feb17 value_total2
rename value_total_jan17 value_total1
reshape long value_total stat_ent_value_total , i(uid) j(month)

count

scalar obs_all = 3*3960


*Statutory NIC entitlement
qui svy: mean stat_ent_value_total if rationcardtype == "AAY" & isurban == 0
qui estat sd
matrix MeanAAY0s = r(mean)

qui svy: mean stat_ent_value_total if rationcardtype == "PH" & isurban == 0
qui estat sd
matrix MeanPH0s = r(mean)

qui svy: mean stat_ent_value_total if rationcardtype == "AAY" & isurban == 1
qui estat sd
matrix MeanAAY1s = r(mean)

qui svy: mean stat_ent_value_total if rationcardtype == "PH" & isurban == 1
qui estat sd
matrix MeanPH1s = r(mean)

scalar avg_stat_ent_value_total = (AAY_weight0*`=MeanAAY0s[1,1]' + PH_weight0*`=MeanPH0s[1,1]' + AAY_weight1*`=MeanAAY1s[1,1]' + PH_weight1*`=MeanPH1s[1,1]')/(AAY_weight0 + PH_weight0 + AAY_weight1 + PH_weight1)


scalar list

gen treatmentXmonth=treatment*month

qui svy: mean value_total if treatment == 0 
qui estat sd
matrix Mean0 = r(mean)

svy: mean value_total if treatment == 1 & b20_adr_seed_hh_0_1 == 1

eststo totalpoolBL1: xi: reg value_total treatment interaction i.strata bl_var bl_var_mi [pw = pweight], cluster(block_code)
estadd scalar control_mean = `=Mean0[1,1]'
estadd scalar stat_ent_mean = avg_stat_ent_value_total
estadd scalar percent_obs = 100*e(N)/obs_all

restore
drop bl_var*


dsconcat `tflist', subset(parm p in 1) dsn(ds_ration) 
qqvalue p, method(yekutieli) qvalue(qval)
mkmat qval

 
forval i = 1/5 {
local rat: word `i' of `ration'
estimates restore `rat'poolBL1


mat bet=e(b)                   

mat bet[1,1]=qval[`i',1]             
mat list bet      
estadd matrix qvalue = bet               
                                            
mat list e(qvalue)
}

			
MultiPartTabPanelStart, ///
			ncol(6) tabname(`tabname') ///
			panelstring("Panel B: Value received")
			
			estimates restore totalpoolBL1 
			estimates restore ricepoolBL1 
			estimates restore wheatpoolBL1 
			estimates restore sugarpoolBL1 
			estimates restore saltpoolBL1 
			estimates restore keropoolBL1
			
MultiPartTabPanelEnd, ///
				ncol(6) tabname(`tabname') ///
				models("totalpoolBL1 ricepoolBL1 wheatpoolBL1 sugarpoolBL1 saltpoolBL1 keropoolBL1") ///
				keep(treatment interaction) varlabels(treatment "Treatment" interaction "Treatment*Seeded",elist(interaction \addlinespace)) ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ])  keep(treatment) fmt(2)) ) ///
				stats(control_mean N percent_obs, label("Control mean" "Observations" "\% of sample") fmt(%12.2g %15.0fc 0)) starlevels( * 0.10 ** 0.05 *** 0.01)			
				
				
**************************************************************
* 3) leakage 
**************************************************************

use "${AdminDataDir}/allocation_blocklevel_jan17_mar17.dta",clear

* keep a separate temp Household data file for merging
preserve
use "${SurveyDataDir}/JH_ePOS_HH_DataforAnalysis.dta",clear
keep ss_code ghost_final district_code block_code fps_code pweight uid treatment isurban strata b16*17 value* *weigh*_y0
tempfile HHdata
save `HHdata'
restore

merge 1:m district_code block_code isurban strata treatment using `HHdata',update  

keep if ss_code == "SS01" | ghost_final == 1 

	
*****************************
* Regression using data pooled across three endline months
*****************************
local tflist ""
scalar obs_all = 3*3960

svyset block_code [pw=pweight]

loc ration "rice wheat sugar salt"
foreach rat of local ration{
preserve
	*BL vars
	gen bl_var_d =  dis_value_perRC_`rat'_y0
	egen bl_var_mean_d = mean(bl_var_d)
	gen bl_var_mi_d = missing(bl_var_d)
	replace bl_var_d = bl_var_mean_d if bl_var_mi_d == 1

	gen bl_var_r = value_`rat'_y0
	egen bl_var_mean_r = mean(bl_var_r)
	gen bl_var_mi_r = missing(bl_var_r)
	replace bl_var_r = bl_var_mean_r if bl_var_mi_r == 1
	
	*Reshape
	keep uid value_`rat'_*17 dis_value_perRC_`rat'_*17 ent_value_perRC_`rat'_*17 rc_count_*17 bl_var_r bl_var_mi_r bl_var_d bl_var_mi_d treatment strata pweight block_code
	rename value_`rat'_mar17 value_`rat'3
	rename value_`rat'_feb17 value_`rat'2
	rename value_`rat'_jan17 value_`rat'1
	rename  dis_value_perRC_`rat'_jan17 dis_value_perRC1
	rename  dis_value_perRC_`rat'_feb17 dis_value_perRC2
	rename  dis_value_perRC_`rat'_mar17 dis_value_perRC3
	rename ent_value_perRC_`rat'_jan17 ent_value_perRC1
	rename ent_value_perRC_`rat'_feb17 ent_value_perRC2
	rename ent_value_perRC_`rat'_mar17 ent_value_perRC3
	rename rc_count_jan17 rc_count1
	rename rc_count_feb17 rc_count2
	rename rc_count_mar17 rc_count3
	reshape long value_`rat' dis_value_perRC ent_value_perRC rc_count, i(uid) j(month)
	
	
	gen treatmentXmonth=treatment*month
	
	svyset block_code [pw=pweight]

	*Disbursement per RC
	qui svy: mean dis_value_perRC if treatment == 0 
	qui estat sd
	matrix Mean0d = r(mean)
	
	*Entitlement per RC
	qui: mean ent_value_perRC if treatment == 0 
	qui estat sd
	matrix Mean0e = r(mean)
	
	
	count if (!missing(dis_value_perRC) & !missing(value_`rat'))   
	scalar used_obs=r(N)         

	svyset block_code [pw=pweight]

	eststo `rat'_dis_pooled_v: xi: svy:reg dis_value_perRC treatment bl_var_d bl_var_mi_d i.strata 
	estadd scalar control_mean = `=Mean0d[1,1]'
	estadd scalar stat_ent_mean = `=Mean0e[1,1]'
	
	*Value received reported by HHs
	qui svy: mean value_`rat' if treatment == 0 
	qui estat sd
	matrix Mean0r = r(mean)
	
	eststo `rat'_rep_pooled_v: xi:svy: reg value_`rat' treatment bl_var_r bl_var_mi_r i.strata 
	estadd scalar control_mean = `=Mean0r[1,1]'
	
	estadd scalar stat_ent_mean = `=Mean0e[1,1]'
	
	*Jointly estimate leakage
	suest `rat'_dis_pooled_v `rat'_rep_pooled_v
	scalar r2_adj = e(r2_a)
	
	eststo pooled_`rat'_v: lincomest[`rat'_dis_pooled_v]treatment - [`rat'_rep_pooled_v]treatment
	estadd scalar control_mean = `=Mean0d[1,1]' - `=Mean0r[1,1]'
	
	
	estadd scalar used_obs=used_obs
    estadd scalar percent_obs = 100*used_obs/obs_all

	estadd scalar r2_adj = e(r2_a)
	estadd scalar stat_ent_mean = `=Mean0e[1,1]'
	
	
	tempfile pooled_`rat'_v
	parmest, label saving("`pooled_`rat'_v'")
	local tflist "`tflist' `pooled_`rat'_v'"
	drop bl_*

restore

}

*Kerosene
preserve
	*BL vars
	gen bl_var_d = dis_value_perRC_kero_y0
	egen bl_var_mean_d = mean(bl_var_d)
	gen bl_var_mi_d = missing(bl_var_d)
	replace bl_var_d = bl_var_mean_d if bl_var_mi_d == 1

	gen bl_var_r = value_kero_y0
	egen bl_var_mean_r = mean(bl_var_r)
	gen bl_var_mi_r = missing(bl_var_r)
	replace bl_var_r = bl_var_mean_r if bl_var_mi_r == 1
	
	*Reshape
		keep uid value_kero_*17 dis_value_perRC_kero_*17 ent_value_perRC_kero_*17 rc_count_*17 bl_var_r bl_var_mi_r bl_var_d bl_var_mi_d treatment strata pweight block_code
	rename value_kero_mar17 value_kero3
	rename value_kero_feb17 value_kero2
	rename value_kero_jan17 value_kero1
	rename  dis_value_perRC_kero_jan17 dis_value_perRC1
	rename  dis_value_perRC_kero_feb17 dis_value_perRC2
	rename  dis_value_perRC_kero_mar17 dis_value_perRC3
	rename ent_value_perRC_kero_jan17 ent_value_perRC1
	rename ent_value_perRC_kero_feb17 ent_value_perRC2
	rename ent_value_perRC_kero_mar17 ent_value_perRC3
	rename rc_count_jan17 rc_count1
	rename rc_count_feb17 rc_count2
	rename rc_count_mar17 rc_count3
	reshape long value_kero dis_value_perRC ent_value_perRC rc_count, i(uid) j(month)
	
	gen treatmentXmonth=treatment*month
	
	count if (!missing(value_kero))   
	scalar used_obs=r(N)  

	
	*Disbursement per RC
	qui svy: mean dis_value_perRC if treatment == 0 
	qui estat sd
	matrix Mean0d = r(mean)
	
	qui mean ent_value_perRC if treatment == 0
	qui estat sd
	matrix Mean0e = r(mean)
	
	svyset block_code[pweight=pweight]
	
	*Quantities received reported by HHs
	qui svy: mean value_kero if treatment == 0 
	qui estat sd
	matrix Mean0r = r(mean)
	
	svyset block_code [pw=pweight]

	eststo kero_rep_pooled_v: xi: svy:reg value_kero treatment bl_var_r bl_var_mi_r i.strata
	estadd scalar control_mean = `=Mean0r[1,1]'
	estadd scalar stat_ent_mean = `=Mean0e[1,1]'

	
	eststo pooled_kero_v: lincomest - treatment
	estadd scalar control_mean = `=Mean0d[1,1]' - `=Mean0r[1,1]'

	estadd scalar used_obs=used_obs
    estadd scalar percent_obs = 100*used_obs/obs_all

	estadd scalar r2_adj = e(r2_a)
	estadd scalar stat_ent_mean = `=Mean0e[1,1]'
	
	tempfile pooled_kero_v
	parmest, label saving("`pooled_kero_v'")
	local tflist "`tflist' `pooled_kero_v'"
	
restore

*Total value

preserve

	*BL vars
	gen bl_var_d =  dis_value_perRC_total_y0
	egen bl_var_mean_d = mean(bl_var_d)
	gen bl_var_mi_d = missing(bl_var_d)
	replace bl_var_d = bl_var_mean_d if bl_var_mi_d == 1

	gen bl_var_r = value_total_y0
	egen bl_var_mean_r = mean(bl_var_r)
	gen bl_var_mi_r = missing(bl_var_r)
	replace bl_var_r = bl_var_mean_r if bl_var_mi_r == 1
	
	*Reshape
	keep uid value_total_*17 dis_value_perRC_total_*17 ent_value_perRC_total_*17 rc_count_*17 bl_var_r bl_var_mi_r bl_var_d bl_var_mi_d treatment strata pweight block_code
	rename value_total_mar17 value_total3
	rename value_total_feb17 value_total2
	rename value_total_jan17 value_total1
	rename dis_value_perRC_total_jan17 dis_value_perRC1
	rename dis_value_perRC_total_feb17 dis_value_perRC2
	rename dis_value_perRC_total_mar17 dis_value_perRC3
	rename ent_value_perRC_total_jan17 ent_value_perRC1
	rename ent_value_perRC_total_feb17 ent_value_perRC2
	rename ent_value_perRC_total_mar17 ent_value_perRC3
	rename rc_count_jan17 rc_count1
	rename rc_count_feb17 rc_count2
	rename rc_count_mar17 rc_count3
	reshape long value_total dis_value_perRC ent_value_perRC rc_count, i(uid) j(month)

  

	gen treatmentXmonth=treatment*month
	
	*Entitlement per RC
	qui: mean ent_value_perRC if treatment == 0 
	qui estat sd
	matrix Mean0e = r(mean)
	
	*Disbursement per RC
	qui svy: mean dis_value_perRC if treatment == 0 
	qui estat sd
	matrix Mean0d = r(mean)
	
	count if (!missing(dis_value_perRC ) & !missing(value_total))   
	scalar used_obs=r(N)  
	
	svyset block_code [pw=pweight]

	eststo total_dis_pooled_v: xi: svy:reg dis_value_perRC treatment bl_var_d bl_var_mi_d i.strata 
	estadd scalar control_mean = `=Mean0d[1,1]'
	estadd scalar stat_ent_mean = `=Mean0e[1,1]'
	
	*Value received reported by HHs
	qui svy: mean value_total if treatment == 0 
	qui estat sd
	matrix Mean0r = r(mean)
    
	eststo total_rep_pooled_v: xi: svy:reg value_total treatment bl_var_r bl_var_mi_r i.strata 
	estadd scalar control_mean = `=Mean0r[1,1]'
	estadd scalar stat_ent_mean = `=Mean0e[1,1]'
	
	*Jointly estimate leakage
	suest total_dis_pooled_v total_rep_pooled_v
	scalar r2_adj = e(r2_a)
	
	eststo pooled_total_v: lincomest[total_dis_pooled_v]treatment - [total_rep_pooled_v]treatment
	estadd scalar control_mean = `=Mean0d[1,1]' - `=Mean0r[1,1]'
	
	estadd scalar used_obs=used_obs
    estadd scalar percent_obs = 100*used_obs/obs_all

	estadd scalar r2_adj = e(r2_a)
	estadd scalar stat_ent_mean = `=Mean0e[1,1]'
	
	drop bl_*
restore




preserve
dsconcat `tflist', subset(parm p in 1) dsn(ds_ration)  
qqvalue p, method(yekutieli) qvalue(qval)
mkmat qval

loc ration "rice wheat sugar salt kero"
forval i = 1/5{
local estname: word `i' of `tflist'
local rat: word `i' of `ration'

estimates restore pooled_`rat'_v


mat bet=e(b)                   

mat bet[1,1]=qval[`i',1]             
mat list bet      
estadd matrix qvalue = bet               
                                            
mat list e(qvalue)
 }

***************** end multi panel table				
MultiPartTabPanelStart, ///
			ncol(6) tabname(`tabname') ///
			panelstring("Panel C: Leakage")
			
MultiPartTabPanelEnd, ///
				ncol(6) tabname(`tabname') ///
				models("pooled_total_v pooled_rice_v pooled_wheat_v pooled_sugar_v pooled_salt_v pooled_kero_v") ///
				varlabels((1) "Treatment" , elist((1) \addlinespace) ) ///
				cells(b(star fmt(%12.2g) ) se(par(( )) fmt(%12.2g) ) qvalue(par([ ]) fmt(2)) ) ///
				starlevels( * 0.10 ** 0.05 *** 0.01) ///
				stats(control_mean used_obs percent_obs, label("Control mean" "Observations" "\% of sample") fmt(%12.2g %15.0fc 0))		
				
				
MultiPartTabEnd, ///
			ncol(6) tabname(`tabname') 
			
